﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using EPIClientRssWP7.AuthenticationService;
using GalaSoft.MvvmLight;
using System.IO.IsolatedStorage;
using GalaSoft.MvvmLight.Messaging;

namespace ViewModel
{
    public class InfoViewModelBase : ViewModelBase
    {

        public InfoViewModelBase()
        {
            authService = new AuthenticationServiceClient();
            clientInfos = ClientInformation.Instance;
            authService.CookieContainer = clientInfos.cc;
        }

        //public event PropertyChangedEventHandler PropertyChanged;
        //protected virtual void RaisePropertyChanged(string propertyName)
        //{
        //    bool manager =  true;
        //    PropertyChangedEventHandler handler = PropertyChanged;
        //    if (handler != null && manager == true)
        //        handler(this, new PropertyChangedEventArgs(propertyName));
        //}

        private AuthenticationServiceClient authService;
        public AuthenticationServiceClient AuthService
        {
            get
            {
                if (authService == null)
                    authService = new AuthenticationServiceClient();
                return authService;
            }
            private set { authService = value; }
        }



        public ClientInformation clientInfos
        { get; set; }


        public ICommand LogOut
        { get; set; }

        public void ExecuteLogOut()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
            AuthService.CookieContainer = clientInfos.cc;
            AuthService.LogoutCompleted += (obj, arg) =>
            {
                if (!(arg != null && arg.Result != null && arg.Error == null))
                    Console.WriteLine("LogOut Failed");
            };
            AuthService.LogoutAsync(new LogoutRequest());
            Messenger.Default.Send<Uri>(new Uri("/MainPage.xaml", UriKind.Relative), "Logout");
        }

        public bool CanExecuteLogOut()
        {
            return true;
        }
    }
}
